#include "ConfigureDialog.h"
#include <QtDebug>

ConfigureDialog::ConfigureDialog( QList<Profile *> * newProfiles ,  QWidget * parent):QDialog(parent){
    //if (profiles == 0) return;
    DEBUG_F();
    profiles = newProfiles;
    profileEdit = 0;
    setupUi(this);
    listWidget->setCurrentRow(0);
    for (int i = 0; i < profiles->size();i++){
	new QListWidgetItem(profiles->at(i)->getName(), profileList);
    }

    options = Options::getGlobalOptions();
    
    fromOptions(isAlarmBoard, "isAlarmBoard");
    fromOptions(isAlarmTopic, "isAlarmTopic");
    fromOptions(isAlarmArticle, "isAlarmArticle");
    fromOptions(isAlarmMessage, "isAlarmMessage");

    fromOptions(isUseDock, "isUseDock");
    fromOptions(isNotUseDock, "isUseDock", false);

    fromOptions(isStayOnSystray, "isStayOnSystray");
    fromOptions(isDoubleClickMode, "isDoubleClickMode");
    
}

void ConfigureDialog::fromOptions(QAbstractButton *button, QString optionName, bool flag){
    if (flag)	button -> setChecked(options->getOption(optionName));
    else	button -> setChecked( ! options->getOption(optionName));
}

void ConfigureDialog::toOptions(QAbstractButton *button, QString optionName, bool flag){
    if (flag)	options->setOption(optionName, button->isChecked());
    else	options->setOption(optionName, ! button->isChecked());
}
    
void ConfigureDialog::on_createButton_clicked(){
    DEBUG_F();
    if (profileEdit == 0){
	qDebug() << ">> Trying to create Profile Configure Window...";
	profileEdit = new ProfileConfigure(this);
    }
    connect (profileEdit, SIGNAL(finished(int)), this, SLOT(createFinish(int)));
    temp = new Profile();
    profileEdit -> set(temp);
    profileEdit -> show();
    profileEdit -> raise();
    profileEdit -> activateWindow();
    profileEdit -> exec();
}

void ConfigureDialog::createFinish(int result){
    DEBUG_F();
    disconnect(profileEdit, SIGNAL(finished(int)), this, SLOT(createFinish(int)));
    if (result == QDialog::Accepted){
	profiles->append(temp);
	profileList -> setCurrentItem(new QListWidgetItem(temp->getName(),profileList));
    }
    else{
	//delete profileList->takeItem(profileList->currentRow());
	delete temp;
    }
}

void ConfigureDialog::on_editButton_clicked(){
    DEBUG_F();
    int current = profileList->currentRow();
    if (current < 0)
	return;
    if (profileEdit == 0){
	profileEdit = new ProfileConfigure(this);
    }
    //connect (profileEdit, SIGNAL(finished(int)), this, SLOT(refresh()));
    temp = profiles->value(current);
    profileEdit ->set(temp);
    profileEdit -> show();
    profileEdit -> raise();
    profileEdit -> activateWindow();
    profileEdit -> exec();
}

void ConfigureDialog::refresh(){
    DEBUG_F();
    //profileList->reset();
}

void ConfigureDialog::on_deleteButton_clicked(){
    DEBUG_F();
    int current = profileList->currentRow();
    if (current < 0)
	return;
    delete profileList->takeItem(current);
    Profile * p = profiles->takeAt(current);
    QSettings settings (PROGRAM_COMP, PROGRAM_NAME);
    settings.beginGroup("profileList");
    p->erase(settings);
    settings.endGroup();
    delete p;
    refresh();
}

void ConfigureDialog::on_buttonBox_accepted(){
    apply();
    emit configureFinished();
    close();
}

void ConfigureDialog::on_buttonBox_rejected(){
    emit configureFinished();
    close();
}

void ConfigureDialog::apply(){
    
    toOptions(isAlarmBoard, "isAlarmBoard");
    toOptions(isAlarmTopic, "isAlarmTopic");
    toOptions(isAlarmArticle, "isAlarmArticle");
    toOptions(isAlarmMessage, "isAlarmMessage");
    
    toOptions(isUseDock, "isUseDock");
    
    toOptions(isStayOnSystray, "isStayOnSystray");
    toOptions(isDoubleClickMode, "isDoubleClickMode");
    
    QSettings globalSettings(PROGRAM_COMP, PROGRAM_NAME);
    globalSettings.beginGroup("Global");
    options->save(globalSettings);
    globalSettings.endGroup();
}